﻿{{include file="../00_include/01_head.tpl"}}
<script type="text/javascript">var base_url = "{{""|base_url}}", edialog, ndialog;</script>
<div class='l-h-28 g-d-w  b-r-t-4 p-l-20 b-d-b-c-ddd p-r dotum'>
    <span>Home</span>
    <span class="arrow_space"></span>
    <span>User</span>
    <div class="p-a t-8 r-8 l-h-16 z-i-100">
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p add-icon" onclick="menu.AddItem()">Add</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p refresh-icon" onclick="menu.Refresh()">Refresh</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p setting-icon" onclick="menu.Setting()">Setting</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p menu-icon" onclick="accessdenied()">Menu</span>
    </div>
</div>
<div class="grid_12- p-a- t-0- l-0- b-0- b-g-c-w- z-i-1 ">
    <div id="gridview" class="frmjqxgrid p-r m-h-500">
        <div id="jqxGrid" style="border: none;"></div>
        <div class="d-p-b h-28 b-d-t-c-ddd"></div>
        <div class="p-a b-4 l-8 l-h-16 z-i-100 p-b-2 dotum">
            <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p add-icon" onclick="menu.AddItem()">Add</span>
            <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p refresh-icon" onclick="menu.Refresh()">Refresh</span>
            <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p setting-icon" onclick="menu.Setting()">Setting</span>
            <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p menu-icon" onclick="accessdenied()">Menu</span>
        </div>
    </div>
</div>
<div id="tree_view" class="b-g-ldg d-p-n"><div id="jqxTree" class="grid_10 h-g-10"></div></div>
{{include file="../base/grid_setting.tpl"}}
<!--<div id="window">
    <div id="windowHeader" >
        <img class="f-l m-r-4" src="/libraries/images/16/fileview_text.png" alt="" />
        <span class="f-l l-h-16">
            Category
        </span>
    </div>
    <div style="overflow: hidden;" id="windowContent">
        <div class="p-r-2 h-i p-b-2 biz">
            Loading...
        </div>
    </div>
</div>-->
<div class="grid_12- d-p-n">
    <div id="editor_box" class="b-d-c-ccc- p-r b-g-c-w- p-4">
        <div class="grid_x biz"></div>
    </div>
</div>
<div class="clear"></div>
<script type="text/javascript">
    var menu=(function(){
        return{
            AddItem:function(){
                _detail('');
            },
            EditItem:function(id){
                _detail(id);
            },
            Save:function(){
                update_ause();
            },
            Cancel:function(){
                $('#editor_box').dialog('close');
            },
            DeleteItem:function(id){
                accessdenied();
            },
            RestoreItem:function(){
                accessdenied();
            },
            Permission:function(id){
                _permission(id);
            },
            PrivateItem:function(){
                accessdenied();
            },
            Setting:function(){
                bckdialog({
                    message:$('#window-sOpt'),
                    icon:"<img class='p-a t-7 l-8' src='"+base_url+"/libraries/images/16/option.gif'/>",
                    title:'Setting'
                }).open();
            },
            Refresh:function(){
                $("#jqxGrid").jqxGrid('updatebounddata');
            }
        };
    }());
    function update_ause(){
        if( $('#ause_frm').validationEngine('validate') === false)return false;
        new backend({
            url         :   "/admincp/backend/user/save",
            data        :   {
                ause_id        :   $("#ause_id").val(),
                ause_name      :   $("#ause_name").val(),
                ause_username  :   $("#ause_username").val(),
                ause_email     :   $("#ause_email").val(),
                ause_password  :   $("#ause_password").val(),
                ause_position  :   $("#ause_position").val(),
                ause_status    :   $("#ause_status").val()
            },
            datatype    :   "json",
            callback    :   function(data){
                if(data.result<0){
                    new bckdialog({type:'error'}).open(data.message);
                }else{
                    uiMessage(data.message);
                }
            }
        }).call();
        return false;
    }
    function randomString() {
        new backend({
            url         :   base_url+"backend/user/randomstring",
            data        :   {},
            datatype    :   "text",
            callback    :   function(data){
                $("#ause_password").val(data);
            }
        }).call();
    }
    function _permission(id){
        backendloading();
        $("#editor_box .biz").load(
        base_url+"backend/user/detail/" + id,
            function() {
                backendloaded();
                bckdialog({
                    title:'<span class="dotum">Change Permission</span>',message:$('#editor_box')
                }).open();
            }
        );
    }
    function _detail(id){
        backendloading();
        $("#editor_box .biz").load(
        "/admincp/backend/user/editor/" + id,
            function() {
                bckdialog({
                    title:'User',message:$('#editor_box')
                }).open();
//                islc({
//                    element:'#cate_parent',num:2,minwidth:'480px',height:'240px',
//                    multi:false,aclass:'_mul',style :{'margin-top':'-120px'},
//                    title:'Parent',filter:true
//                });
//                $( "#tabs" ).tabs();
                backendloaded();
                $("#ause_frm").validationEngine();
            }
        );
    }
    function AliasTo(from,element){
        if($(from).val()==$(from+'_curent').val())return;
        var url=base_url+"backend/excution/getalias";
        var data={text:$(from).val()};
        var proc=new backend({
            url:url,data:data,
            callback:function(rsdata){
                if(rsdata.result===1){
                    $(element).val(rsdata.alias);
                    $(from+'_curent').val($(from).val());
                }else{
                    bckdialog().open(rsdata.message);
                }
            }
        }).call();
    }
    function changeprivilege(user_id,privilege_id,value,secretkey,signature){
        //if(getCheckedRadio(id)!==value){
        new backend({
            url         :   base_url+"backend/user/setprivilege",
            data        :   {
                user_id		:	user_id,
                privilege_id:	privilege_id,
                permission	:	value,
                secretkey	:	secretkey,
                signature	:	signature
            },
            datatype    :   "json",
            callback    :   function(data){
                try{
                    if(data.result<0){
                        bckdialog({
                            type:'error'
                        }).open(data.message);
                    }else{
                        uiMessage(data.message);
                    }
                }catch(e){
                    bckdialog({
                        type:'error'
                    }).open("Sorry. Your request could not be completed.<br/> Please check your input data and try again.");
                }
                console.log(data);
            }
        }).call();
    //}
    }
    function getCheckedRadio(name) {
        var elements = document.getElementsByName(name);

        for (var i=0, len=elements.length; i<len; ++i)
            if (elements[i].checked) return elements[i].value;
        return '';
    }
    var grid_options,
        adatafiels  ={{$datafields|json_encode}},
        acolumns    ={{$columns|json_encode}},
        alistitems  ={{$list|json_encode}},
        jqxGrid;
    var bindingsource='{{$bindingsource}}';

    
    function handleEvents(){
        try{
            acolumns['_id'].cellsrenderer = function (row, column, value) {
                var str="<span style='margin: 4px; float: left;'>";
                if(value && value>0){
                    try{
                        //value = $.parseJSON(value);
                        str+="\
                            <div onclick=\"menu.EditItem('"+value+"');\" \
                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 edit-icon' title='Edit'></div>\
                        ";
                        str+="\
                            <div onclick=\"menu.DeleteItem('"+value+"');\" \
                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 del-icon' title='Delete'></div>\
                        ";
                        str+="\
                            <div onclick=\"menu.Permission('"+value+"');\" \
                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 user-icon' title='Permission'></div>\
                        ";
                    }catch(e){}
                }
                str+="</span>";
                return str;
            }
            
            $("#jqxGrid").bind('cellendedit', function (event) {
                try{
                    var args = event.args;
                    var column = args.datafield ,_row = args.rowindex,_value = args.value;
                    var _data=$("#jqxGrid").jqxGrid('getrowdata', _row);
                    var _id=_data._id;
                    if(_id==undefined || _id==""){
                        return;
                    }
                    switch(column){
                        case "ause_status":
                            if(_value=="true" || _value ==true) _value="true";
                            else _value="false";
                            backend_update('user','auth_users','ause',_id,{'ause_status':_value});
                            break;
                        default:
                        alert("Unkown this cell. Maybe 'editable' of this colums is 'off'."); 
                    }
                }catch(e){
                    alert(e.message);
                }
            });
        }catch(e){}
    }
    
    $(document).ready(function(){
        initSetting();
        Grid({
            grid_opt:{
                //groupable: true,groups: ['cate_parent']
                editable:true,editmode:'dblclick',
                selectionmode: 'singlecell'
            }
        }).init();
        
    });
</script>
<script src="{{base_url()}}/libraries/backend/backend.grid.default.js"></script>
{{include file="../00_include/02_foot.tpl"}}
